package com.hqhsc.module.front.activity.controller;

import com.hqhsc.module.front.activity.service.SignService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/activity/sign")
public class SignController {
	final Logger logger = LoggerFactory.getLogger(SignController.class);

    @Autowired
    private SignService signService;

    @RequestMapping(value = "/index" , method = RequestMethod.GET)
    public String activityUserSign(Model model , HttpServletRequest request , HttpServletResponse response) {
        String activity_id_str = request.getParameter("aid");
        logger.info("sign index aid: " + activity_id_str);
        model.addAttribute("aid" , activity_id_str);
        return "/module/front/activity/sign/signindex";
    }


    /**
     * 活动用户，现场扫码签到，输入手机号，更新签到状态
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/dosign" , method = RequestMethod.POST)
    public String activityUserDoSign(Model model , HttpServletRequest request , HttpServletResponse response) {

        String activity_id_str = request.getParameter("aid");
        String dn = request.getParameter("dn");
        logger.info("activityUserSign aid: " + activity_id_str + " dn: " + dn);
        int result = this.signService.activityUserDoSign(Long.valueOf(activity_id_str) , dn);
        if(result > 0) {
            return "/module/front/activity/sign/signsuccess";
        } else {
            model.addAttribute("aid" , activity_id_str);
            return "/module/front/activity/sign/signfailed";
        }
    }
}